package com.zat.mes.andrmes.dofun.work


import android.content.Intent
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.zxing.common.StringUtils
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.dofun.work.adapter.GetBomNotifiAdapter
import com.zat.mes.andrmes.dofun.work.vm.GetBomNotifiModel
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_get_bom_notifi.*
import kotlinx.android.synthetic.main.layout_title.*

class GetBomNotifiActivity : BaseActivity() {

    var getBomNotifiModel: GetBomNotifiModel? = null
    var getBomNotifiAdapter: GetBomNotifiAdapter? = null
    override fun init() {
        initView()
        initData()
    }

    private fun initData() {
        getBomNotifiModel = ViewModelProvider(this).get(GetBomNotifiModel::class.java)
        getBomNotifiModel?.getJiagongData()?.observe(this, Observer {
            closeLoading()
            getBomNotifiAdapter?.setNewData(it)
        })

    }

    private fun initView() {
        setContentView(R.layout.activity_get_bom_notifi)
        tVtitle.text = "原料领用通知单"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        btnSearch.setOnClickListener {
            if (TextUtils.isEmpty(etOrder.text.toString())) {
                showToast("请输入制单号或加工单号")
                return@setOnClickListener
            }
            getBomNotifiModel?.getJiagong(etOrder.text.toString())
            showLoading()
        }
        recyclerView.layoutManager = LinearLayoutManager(this)
        getBomNotifiAdapter = GetBomNotifiAdapter(R.layout.item_get_bom_notifi)
        recyclerView.adapter = getBomNotifiAdapter
        recyclerView.addItemDecoration( DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        getBomNotifiAdapter?.setOnItemClickListener { adapter, view, position ->
            var intent = Intent(this,GetBomNotifiDetailActivity::class.java)
            intent.putExtra("yarnInformNo",getBomNotifiAdapter?.getItem(position)?.yarnInformNo)
            startActivity(intent)
        }
    }
}